package com.situ.springbootscientific.controller;

import cn.hutool.extra.servlet.JakartaServletUtil;
import com.situ.springbootscientific.pojo.Auth;
import com.situ.springbootscientific.pojo.Researchers;
import com.situ.springbootscientific.pojo.query.ResearchersQuery;
import com.situ.springbootscientific.pojo.query.ResearchersQuery;
import com.situ.springbootscientific.pojo.vo.ResearchIdVo;
import com.situ.springbootscientific.service.IAuthService;
import com.situ.springbootscientific.service.IResearchersService;
import com.situ.springbootscientific.utils.JSONUtil;
import com.situ.springbootscientific.utils.PageRusults;
import com.situ.springbootscientific.utils.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/researchers")
public class ResearchersController {
    @Autowired
    IResearchersService researchersService;
    @Autowired
    IAuthService authService;
    @RequestMapping("/login")
    @ResponseBody
    public Result login(String name, String password, HttpSession session, String code , String autoLogin,HttpServletRequest request,HttpServletResponse response ) {
        System.out.println("name:"+name+",password:"+password+",code:"+code+",autoLogin:"+autoLogin);
        //在所有登陆操作之前,先检查验证码是否为空
        String codeInSession = (String) session.getAttribute("codeInSession");
        System.out.println(codeInSession);
        if (!codeInSession.equals(code.toLowerCase())){
            Result result = new Result(1,"验证码错误");

            return result;
        }
        Researchers researchers = researchersService.login(name, password);
        if (researchers == null) {
            return Result.error("用户名或密码错误");
        }
        if (researchers.getStatus() == 0) {
            return Result.error("该用户已经被禁用");
        }

        //登录成功
        session.setAttribute("researchers", researchers);

        //判断是否勾选了自动登录
        if (autoLogin != null && !"".equals(autoLogin)){
            //下面是添加cookie的代码
            //cookie是以键值对的形式存在
            Cookie cookie = new Cookie("autoLogin",researchers.getName()+"&"+researchers.getPassword());

            //设置cookie存活时间和路径
            cookie.setMaxAge(60*60*24*7);//一周
            cookie.setPath("/");
            //cookie已经设置好了，把它添加到响应中，返回给浏览器吧
            response.addCookie(cookie);
        }

        //在这里查权限试试
        System.out.println("role:"+researchers.getRole());
        List<Auth> authList = authService.findAll(researchers.getRole());
        System.out.println(authList);
        session.setAttribute("authList", authList);
        return Result.ok("登录成功");


    }
    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("researchers");
        return Result.ok();
    }

    @RequestMapping("/list")
    public PageRusults list(ResearchersQuery researchersQuery) {
        PageRusults pageRusults = researchersService.list(researchersQuery);
        return pageRusults;
    }

    @RequestMapping("/selectById")
    public PageRusults selectById(HttpServletRequest request,HttpSession session) {
        Researchers researchers = (Researchers) session.getAttribute("researchers");
        Integer id = researchers.getId();
        List<ResearchIdVo> researchIdVo = researchersService.selectById(id);
        return new PageRusults(0,"",1,researchIdVo);
    }
    @RequestMapping("/avatar")
    public Result avatar(Researchers researchers){
        researchersService.avatar(researchers);
        return Result.ok();
    }

}
